import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Queue;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * Application entry point
 * @author Jeffrey Wong
 * @author Dori Saba
 * @author Ajay Sabhaney
 */
public class SystemExplorer extends JFrame {
	
	private JDesktopPane explorerWindow;
	private Logo background;
	
	private Color lightBlue = new Color(184, 207, 229, 255);

	private UserLogin login;
	private PatientExplorer patients;
	private MedicalRecordExplorer medicalExplorer;
	
	private JMenuItem loginMenuButton;
	
	private String user;
	private int accessLevel = -1;
	
	//patient queue
	private JPanel bottomPanel;
	private JPanel patientQueuePanel;
	private Vector<Patient> waitingPatients;
	private int lowPriorityIndex = 0, mediumPriorityIndex = 0, highPriorityIndex = 0;
	
	public SystemExplorer() {
		
		explorerWindow = new JDesktopPane();
		
		JMenuBar menuBar = new JMenuBar();
		
		//the file menu
		JMenu fileMenu = new JMenu("File");
		menuBar.add( fileMenu );
		loginMenuButton = new JMenuItem("Logout");
		loginMenuButton.setEnabled(false);
		fileMenu.add( loginMenuButton );
		loginMenuButton.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent v) {
				JInternalFrame[] windows = explorerWindow.getAllFrames();
				for( int i = 0; i < windows.length; i++ ) {
					windows[i].setVisible(false);
					explorerWindow.remove( windows[i] );
				}
				loginMenuButton.setEnabled(false);
				patientQueuePanel.setVisible(false);
				showLogin();
			}
		});
		JMenuItem exitMenuButton = new JMenuItem("Exit");
		fileMenu.add( exitMenuButton );
		exitMenuButton.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent c) {
				System.exit(0);
			}
		});
		
		
		explorerWindow.setBackground( Color.WHITE );
		
		int x = getSize().width;
		int y = getSize().height;

		background = new Logo(x, y);
		explorerWindow.add( background, BorderLayout.EAST );
		background.setVisible(true);
		explorerWindow.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

		addComponentListener(new ComponentAdapter() {

			public void componentResized(ComponentEvent evt) {
				explorerWindow.remove(background);
				int xC = getSize().width;
				int yC = getSize().height;
				background = new Logo(xC, yC);
				explorerWindow.add( background, BorderLayout.EAST );
				background.setVisible(true);
			}

		});
		
		
		this.setJMenuBar( menuBar );
		getContentPane().setLayout( new BorderLayout() );
		
		getContentPane().add( explorerWindow );
		
		bottomPanel = new JPanel( new BorderLayout() );
		bottomPanel.setBackground(Color.WHITE);
		
		waitingPatients = new Vector<Patient>();
		patientQueuePanel = new JPanel();
		patientQueuePanel.setBackground( Color.WHITE );
		patientQueuePanel.add( Box.createRigidArea( new Dimension(1,15) ) );
		JLabel queueLabel = new JLabel( "Patients Waiting:" );
		patientQueuePanel.add( queueLabel );
		queueLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
		patientQueuePanel.setVisible(false);
		
		
		
		bottomPanel.add( patientQueuePanel, BorderLayout.WEST );
		getContentPane().add( bottomPanel, BorderLayout.SOUTH );
		
		
		this.setSize( 1000, 1000 );
		this.setVisible( true );
		
		showLogin();
		
	}
	
	private void showLogin() {
		login = new UserLogin();
		explorerWindow.add( login );
		login.moveToFront();
		try {
			login.setSelected(true);
		}
		catch(Exception a) {}
		
		login.loginButton.addActionListener( 
				new ActionListener() { 
						public void actionPerformed(ActionEvent evt) {
							String pass = new String(login.password.getPassword());
					    	if ( login.employeeID.getText().trim().equals("username") && pass.equals("iloveadjunct" ) ) {
					    		login(login.employeeID.getText().trim());
					    	}
					    	else {
					    		login.status.setText("Invalid login - please try again or see system admin");
					    		login.password.setText("");
					    	}
						}
				}
		);
		login.password.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						String pass = new String(login.password.getPassword());
				    	if ( login.employeeID.getText().trim().equals("username") && pass.equals("iloveadjunct" ) ) {
				    		login(login.employeeID.getText().trim());
				    	}
				    	else {
				    		login.status.setText("Invalid login - please try again or see system admin");
				    		login.password.setText("");
				    	}
					}
				}
		);
	}
	
	private void login( String id ) {
		user = id;
		accessLevel = 3;
		
		patientQueuePanel.setVisible(true);
		
		login.setVisible(false);
		explorerWindow.remove( login );
		
		loginMenuButton.setEnabled(true);
		
		patients = new PatientExplorer();
		explorerWindow.add( patients );
		patients.moveToFront();
		try {
			patients.setSelected(true);
		}
		catch(Exception a) {}
		patients.viewPatientButton.addActionListener( new ActionListener() {
			public void actionPerformed( ActionEvent evt ) {
				int r = patients.patientTable.getSelectedRow();
				medicalExplorer = new MedicalRecordExplorer( patients.getPatientFromTable(r) );
				explorerWindow.add( medicalExplorer );
				medicalExplorer.moveToFront();
				try {
					medicalExplorer.setSelected(true);
				}
				catch(Exception a) {}
			}
		});
		
		patients.checkinPatientButton.addActionListener( new ActionListener() {
			public void actionPerformed( ActionEvent evt ) {
				int r = patients.patientTable.getSelectedRow();
				Object[] possibilities = {"Normal", "Medium", "Immediate"};
				String s = (String)JOptionPane.showInputDialog(
				                    patients,
				                    "What is the priority of this patient?",
				                    "Patient Priority",
				                    JOptionPane.PLAIN_MESSAGE,
				                    null,
				                    possibilities,
				                    "Normal");

				
				Patient  p = patients.getPatientFromTable(r);
				if ( s != null ) {
					if ( s.equals("Immediate")) {
						waitingPatients.add( p );
						lowPriorityIndex++;
						mediumPriorityIndex++;
						highPriorityIndex++;
						
						JButton newPatientButton = new JButton( p.getFirstName() );
						patientQueuePanel.setVisible(false);
						newPatientButton.setForeground(Color.RED);
						newPatientButton.setOpaque(false);
						newPatientButton.setBackground(new java.awt.Color(0,100,255));
						patientQueuePanel.add( newPatientButton, highPriorityIndex+1 );
						patientQueuePanel.setVisible(true);
						
						newPatientButton.addActionListener( new ActionListener() {
							public void actionPerformed(ActionEvent e) {
								MedicalRecordExplorer medicalExplorer = new MedicalRecordExplorer( patients.getPatientFromFirstName( ((JButton)e.getSource()).getText() ) );
								explorerWindow.add( medicalExplorer );
								medicalExplorer.moveToFront();
								try {
									medicalExplorer.setSelected(true);
								}
								catch(Exception a) {}
								patientQueuePanel.setVisible(false);
								patientQueuePanel.remove( (JButton)e.getSource() );
								lowPriorityIndex--;
								mediumPriorityIndex--;
								highPriorityIndex--;
								patientQueuePanel.setVisible(true);
							}
						});
						
					}
					else if ( s.equals("Medium") ) {
						waitingPatients.add( p );
						lowPriorityIndex++;
						mediumPriorityIndex++;
						
						JButton newPatientButton = new JButton( p.getFirstName() );
						patientQueuePanel.setVisible(false);
						newPatientButton.setForeground(Color.GREEN);
						newPatientButton.setOpaque(false);
						newPatientButton.setBackground(new java.awt.Color(0,100,255));
						patientQueuePanel.add( newPatientButton, mediumPriorityIndex+1 );
						patientQueuePanel.setVisible(true);
						
						newPatientButton.addActionListener( new ActionListener() {
							public void actionPerformed(ActionEvent e) {
								MedicalRecordExplorer medicalExplorer = new MedicalRecordExplorer( patients.getPatientFromFirstName( ((JButton)e.getSource()).getText() ) );
								explorerWindow.add( medicalExplorer );
								medicalExplorer.moveToFront();
								try {
									medicalExplorer.setSelected(true);
								}
								catch(Exception a) {}
								patientQueuePanel.setVisible(false);
								patientQueuePanel.remove( (JButton)e.getSource() );
								lowPriorityIndex--;
								mediumPriorityIndex--;
								patientQueuePanel.setVisible(true);
							}
						});
					}
					else if ( s.equals("Normal") ) {
						waitingPatients.add( p );
						lowPriorityIndex++;
						
						JButton newPatientButton = new JButton( p.getFirstName() );
						patientQueuePanel.setVisible(false);
						newPatientButton.setForeground(Color.BLACK);
						newPatientButton.setOpaque(false);
						newPatientButton.setBackground(new java.awt.Color(0,100,255));
						patientQueuePanel.add( newPatientButton, lowPriorityIndex+1 );
						//patientQueuePanel.add( newPatientButton );
						patientQueuePanel.setVisible(true);
						
						newPatientButton.addActionListener( new ActionListener() {
							public void actionPerformed(ActionEvent e) {
								MedicalRecordExplorer medicalExplorer = new MedicalRecordExplorer( patients.getPatientFromFirstName( ((JButton)e.getSource()).getText() ) );
								explorerWindow.add( medicalExplorer );
								medicalExplorer.moveToFront();
								try {
									medicalExplorer.setSelected(true);
								}
								catch(Exception a) {}
								patientQueuePanel.setVisible(false);
								patientQueuePanel.remove( (JButton)e.getSource() );
								lowPriorityIndex--;
								patientQueuePanel.setVisible(true);
							}
						});
					}
				}
				


			}
		});
		
	}
	
	public static void main( String[] args ) {
		SystemExplorer app = new SystemExplorer();
		app.addWindowListener( 
			new WindowAdapter() {
				public void windowClosing( WindowEvent e ) {
					System.exit(0);
				}
			} 
		);
	}	
}


